/*给定头节点，将小于头节点的节点按照原顺序置于头结点之前*/
/*将小于头节点的节点头插到头节点之前*/
public class Splitlinklist {
    public static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode Splitlinklist(ListNode head){
        ListNode cur = head.next;
        //先找到第一个头插的节点
        ListNode pre = null;
        ListNode newHead = head;
        for (;;cur = cur.next){
            if (cur.val < head.val){
                ListNode next = cur.next;
                cur.next = head;
                pre = cur;
                cur = next;
                break;
            }else {

            }

            break;
        }
        newHead = pre;

        while (cur != null){
            if (cur.val < head.val){
                ListNode next = cur.next;
                cur.next = head;
                pre.next = cur;

                cur = next;
                pre = pre.next;
            }
        }
        if (cur == null && newHead == head){
            return head;
        }
        return newHead;
    }

    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(90);
        ListNode listNode2 = new ListNode(500);
        ListNode listNode3 = new ListNode(95);
        ListNode listNode4 = new ListNode(555);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        Splitlinklist s = new Splitlinklist();
        ListNode newHead = s.Splitlinklist(listNode1);
        System.out.println(newHead.val);
    }
}
